package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mqtt.domain.CustomMqttClient;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.IotElevatorOss;
import org.dromara.iot.domain.bo.IotElevatorBo;
import org.dromara.iot.domain.vo.IotElevatorVo;
import org.dromara.iot.service.IIotElevatorService;
import org.dromara.system.domain.vo.SysOssVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯管理
 *
 * @author LSL
 * @date 2024-06-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/elevator")
public class IotElevatorController extends BaseController {

    private final IIotElevatorService iotElevatorService;
    private final CustomMqttClient customMqttClient;

    /**
     * 查询电梯管理列表
     */
    @SaCheckPermission("iot:elevator:list")
    @GetMapping("/list")
    public TableDataInfo<IotElevatorVo> list(IotElevatorBo bo, PageQuery pageQuery) {
        return iotElevatorService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯管理列表
     */
    @SaCheckPermission("iot:elevator:export")
    @Log(title = "电梯管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotElevatorBo bo, HttpServletResponse response) {
        List<IotElevatorVo> list = iotElevatorService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯管理", IotElevatorVo.class, response);
    }

    /**
     * 获取电梯管理详细信息
     *
     * @param elevatorId 主键
     */
    @SaCheckPermission("iot:elevator:query")
    @GetMapping("/{elevatorId}")
    public R<IotElevatorVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long elevatorId) {
        return R.ok(iotElevatorService.queryById(elevatorId));
    }

    /**
     * 新增电梯管理
     */
    @SaCheckPermission("iot:elevator:add")
    @Log(title = "电梯管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotElevatorBo bo) {
        return toAjax(iotElevatorService.insertByBo(bo));
    }

    /**
     * 修改电梯管理
     */
    @SaCheckPermission("iot:elevator:edit")
    @Log(title = "电梯管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotElevatorBo bo) {
        return toAjax(iotElevatorService.updateByBo(bo));
    }

    /**
     * 删除电梯管理
     *
     * @param elevatorIds 主键串
     */
    @SaCheckPermission("iot:elevator:remove")
    @Log(title = "电梯管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{elevatorIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] elevatorIds) {
        return toAjax(iotElevatorService.deleteWithValidByIds(List.of(elevatorIds), true));
    }

    /**
     * 电梯展示端强制刷新
     */
    @GetMapping("/refresh/{elevatorId}")
    public R<Void> refresh(@NotNull(message = "主键不能为空") @PathVariable Long elevatorId) {
        return toAjax(iotElevatorService.refresh(elevatorId));
    }

    /**
     * 查询已分配oss列表
     */
    @GetMapping("/oss/allocatedList")
    public TableDataInfo<SysOssVo> allocatedList(@Validated(QueryGroup.class) IotElevatorBo bo, PageQuery pageQuery) {
        return iotElevatorService.selectAllocatedList(bo, pageQuery);
    }

    /**
     * 查询未分配用户oss列表
     */
    @GetMapping("/oss/unallocatedList")
    public TableDataInfo<SysOssVo> unallocatedList(@Validated(QueryGroup.class) IotElevatorBo bo, PageQuery pageQuery) {
        return iotElevatorService.selectUnallocatedList(bo, pageQuery);
    }

    /**
     * 取消分配oss
     */
    @PutMapping("/oss/cancel")
    public R<Void> cancelOss(@RequestBody IotElevatorOss elevatorOss) {
        return toAjax(iotElevatorService.deleteOss(elevatorOss));
    }

    /**
     * 批量取消分配oss
     */
    @PutMapping("/oss/cancelAll")
    public R<Void> cancelAllOss(Long elevatorId, Long[] ossIds) {
        return toAjax(iotElevatorService.deleteOssList(elevatorId, ossIds));
    }

    /**
     * 批量分配oss
     */
    @PutMapping("/oss/assignAll")
    public R<Void> assignAllOss(Long elevatorId, Long[] ossIds) {
        return toAjax(iotElevatorService.insertOssList(elevatorId, ossIds));
    }

    /**
     * 开启板端模型检测
     */
    @SaIgnore
    @GetMapping("/modelDetectionEnabled")
    public R<Void> modelDetectionEnabled(@RequestParam("registeredCode") String registeredCode, @RequestParam("rtspUrl") String rtspUrl) {
        String topic = "elevator/event/" + registeredCode + "/model_detection_enabled";
        JSONObject json = JSONUtil.createObj();
        json.set("detect_flag", 1);
        json.set("rtsp_url", rtspUrl);
        customMqttClient.publish(false, topic, json.toString());
        return R.ok();
    }
}
